package com.ruoyi.project.system.statistics.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.grouping.domain.Grouping;
import com.ruoyi.project.system.grouping.service.IGroupingService;
import com.ruoyi.project.system.list.service.IAccountListService;
import com.ruoyi.project.system.user.domain.User;
import com.sun.net.httpserver.Authenticator;
import org.apache.poi.ss.formula.functions.Mode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.statistics.domain.AgainstStatistics;
import com.ruoyi.project.system.statistics.service.IAgainstStatisticsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-04-13
 */
@Controller
@RequestMapping("/system/statistics")
public class AgainstStatisticsController extends BaseController
{
    private String prefix = "system/statistics";

    @Autowired
    private IAgainstStatisticsService againstStatisticsService;
    @Autowired
    private IAccountListService accountListService;
    @Autowired
    private IGroupingService groupingService;

//    @RequiresPermissions("system:statistics:view")
    @GetMapping()
    public String statistics(AgainstStatistics againstStatistics,Model model)
    {
//        Integer accountCount = againstStatisticsService.selectAccountCount();
//        String yesterdayEarnings = againstStatisticsService.yesterdayEarnings();
//        String todayEarnings = againstStatisticsService.todayEarnings();
//        String allEarnings = againstStatisticsService.allEarnings();
//        List<Grouping> groupings = groupingService.selectGrouping();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
//        model.addAttribute("groupings",groupings);
        if ("admin".equals(loginUser)){
            List<Grouping> groupings = groupingService.selectGrouping();
            model.addAttribute("groupings",groupings);
        }else {
            List<Grouping> groupings = groupingService.selectByUserName(loginUser);
            model.addAttribute("groupings",groupings);
            List<AgainstStatistics> againstStatisticsList = againstStatisticsService.selectAgainstStatisticsByUserName(againstStatistics);
            model.addAttribute("againstStatisticsList",againstStatisticsList);
        }
//        model.addAttribute("accountCount",accountCount);
//        model.addAttribute("yesterdayEarnings",yesterdayEarnings);
//        model.addAttribute("todayEarnings",todayEarnings);
//        model.addAttribute("allEarnings",allEarnings);
        return prefix + "/statistics";
    }


    //跳转详情页面
    @GetMapping("/particulars/{accountNumber}")
    public String particulars(@PathVariable("accountNumber") String accountNumber,Model model,String startTime,String endTime){

        model.addAttribute("accountNumber",accountNumber);
        model.addAttribute("startTime",startTime);
        model.addAttribute("endTime",endTime);
        return prefix + "/particulars";
    }





    /**
     * 查询【请填写功能名称】列表
     */
//    @RequiresPermissions("system:statistics:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgainstStatistics againstStatistics)
    {
        startPage();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if ("admin".equals(loginUser)){
            List<AgainstStatistics> list = againstStatisticsService.selectAll(againstStatistics);
            return getDataTable(list);
        }else {
//            String accountNumber = againstStatistics.getAccountNumber();
//            String hme = againstStatistics.getHme();
//            String againstResults = againstStatistics.getAgainstResults();
//            String againstType = againstStatistics.getAgainstType();
//            AgainstStatistics againstStatisticsByUserName=new AgainstStatistics();
//            againstStatisticsByUserName.setUserName(loginUser);
//            againstStatisticsByUserName.setAccountNumber(accountNumber);
//            againstStatisticsByUserName.setHme(hme);
//            againstStatisticsByUserName.setAgainstResults(againstResults);
//            againstStatisticsByUserName.setAgainstType(againstType);
              againstStatistics.setUserName(loginUser);
            List<AgainstStatistics> againstStatisticsListByUserName = againstStatisticsService.selectAgainstStatisticsByUserName(againstStatistics);
            return getDataTable(againstStatisticsListByUserName);
        }
        //        List<AgainstStatistics> list = againstStatisticsService.selectAgainstStatisticsList(againstStatistics);
    }

    @PostMapping("/particularsList")
    @ResponseBody
    public TableDataInfo particularsList(@RequestParam("accountNumber") String accountNumber,String startTime,String endTime)
    {
        startPage();
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String beginTime = request.getParameter("beginTime");
//        String endTime = request.getParameter("endTime");
        HashMap map=new HashMap<String,String>();
        map.put("beginTime",startTime);
        map.put("endTime",endTime);
//        List<AgainstStatistics> list = againstStatisticsService.selectAgainstStatisticsList(againstStatistics);
        AgainstStatistics againstStatistics =new AgainstStatistics();
        againstStatistics.setAccountNumber(accountNumber);
        List<AgainstStatistics> list = againstStatisticsService.selectParticulars(againstStatistics,map);
        return getDataTable(list);
    }


    /**
     * 导出【请填写功能名称】列表
     */
//    @RequiresPermissions("system:statistics:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AgainstStatistics againstStatistics)
    {
        List<AgainstStatistics> list = againstStatisticsService.selectAgainstStatisticsList(againstStatistics);
        ExcelUtil<AgainstStatistics> util = new ExcelUtil<AgainstStatistics>(AgainstStatistics.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
//    @RequiresPermissions("system:statistics:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AgainstStatistics againstStatistics)
    {
//        // 获取当前的用户信息
//        User currentUser = ShiroUtils.getSysUser();
//        // 获取当前的用户名称
//        String loginUser = currentUser.getLoginName();
//        String accountNumber = againstStatistics.getAccountNumber();
//        String hme = againstStatistics.getHme();
//        String againstResults = againstStatistics.getAgainstResults();
//        String againstType = againstStatistics.getAgainstType();
//        AgainstStatistics againstStatisticsAdd = new AgainstStatistics();
//        againstStatisticsAdd.setUserName(loginUser);
//        againstStatisticsAdd.setAccountNumber(accountNumber);
//        againstStatisticsAdd.setHme(hme);
//        againstStatisticsAdd.setAgainstResults(againstResults);
//        againstStatisticsAdd.setAgainstType(againstType);
        return toAjax(againstStatisticsService.insertAgainstStatistics(againstStatistics));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @RequiresPermissions("system:statistics:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AgainstStatistics againstStatistics = againstStatisticsService.selectAgainstStatisticsById(id);
        mmap.put("againstStatistics", againstStatistics);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
//    @RequiresPermissions("system:statistics:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AgainstStatistics againstStatistics)
    {
        return toAjax(againstStatisticsService.updateAgainstStatistics(againstStatistics));
    }



    /**
     * 删除【请填写功能名称】
     */
//    @RequiresPermissions("system:statistics:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(againstStatisticsService.deleteAgainstStatisticsByIds(ids));
    }


    @PostMapping("/selectHmeByTime")
    @ResponseBody
    public Object selectHmeByTime(AgainstStatistics againstStatistics){
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        System.out.println(againstStatistics);
//        model.addAttribute("groupings",groupings);
        if ("admin".equals(loginUser)){
            String allHme = againstStatisticsService.selectHmeByTime(againstStatistics);
            return  allHme==null?"0":allHme;
        }else {
            againstStatistics.setUserName(loginUser);
            String allHme = againstStatisticsService.selectHmeByTime(againstStatistics);
            return allHme==null?"0":allHme;
        }
    }
}
